﻿Imports System.Net
Imports System.IO
Imports System.Xml
Namespace ImgUrNET
    Class ImgUrNET
        Dim xml As String
        Dim url As String
        Dim del As String
        ''' <value>The raw output XML.</value>
        ReadOnly Property RawXML As String
            Get
                Return xml
            End Get
        End Property
        ''' <value>The URL to the uploaded image.</value>
        ReadOnly Property ImageURL As String
            Get
                Return url
            End Get
        End Property
        ''' <value>The URL to the uploaded image.</value>
        ReadOnly Property ImageDeleteCode As String
            Get
                Return del
            End Get
        End Property
        ''' <summary>
        ''' The Upload from file sub.
        ''' </summary>
        ''' <param name="Filename">The local filename to the image.</param>
        ''' <param name="APIKey">Your anonymous API Key to ImgUr.</param>
        Sub UploadFile(ByVal Filename As String, ByVal APIKey As String)
            Dim imageData As Byte()
            Dim fileStream As FileStream = File.OpenRead(Filename)
            imageData = New Byte(fileStream.Length) {}
            fileStream.Read(imageData, 0, imageData.Length)
            fileStream.Close()
            Dim uploadRequestString As [String] = URLEncode("image") + "=" + URLEncode(System.Convert.ToBase64String(imageData)) + "&" + URLEncode("key") + "=" + URLEncode(APIKey) + "&" + URLEncode("type") + "=" + URLEncode("base64")
            Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://api.imgur.com/2/upload"), HttpWebRequest)
            webRequest.Method = "POST"
            webRequest.ContentType = "application/x-www-form-urlencoded"
            webRequest.ServicePoint.Expect100Continue = False
            Dim streamWriter As New StreamWriter(webRequest.GetRequestStream())
            streamWriter.Write(uploadRequestString)
            streamWriter.Close()
            Dim response As WebResponse = webRequest.GetResponse()
            Dim responseStream As Stream = response.GetResponseStream()
            Dim responseReader As New StreamReader(responseStream)
            Dim responseString As String = responseReader.ReadToEnd()
            xml = responseString
            Dim xml1 As New XmlDocument
            xml1.LoadXml(responseString)
            Dim node As XmlNodeList = xml1.GetElementsByTagName("original")
            url = node(0).InnerText
            node = xml1.GetElementsByTagName("delete_page")
            del = node(0).InnerText
        End Sub
        ''' <summary>
        ''' The Upload from URL sub.
        ''' </summary>
        ''' <param name="URL">The URL to the image.</param>
        ''' <param name="APIKey">Your anonymous API Key to ImgUr.</param>
        Sub UploadURL(ByVal URL As String, ByVal APIKey As String)
            Dim uploadRequestString As [String] = URLEncode("image") + "=" + URLEncode(URL) + "&" + URLEncode("key") + "=" + URLEncode(APIKey) + "&" + URLEncode("type") + "=" + URLEncode("url")
            Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://api.imgur.com/2/upload"), HttpWebRequest)
            webRequest.Method = "POST"
            webRequest.ContentType = "application/x-www-form-urlencoded"
            webRequest.ServicePoint.Expect100Continue = False
            Dim streamWriter As New StreamWriter(webRequest.GetRequestStream())
            streamWriter.Write(uploadRequestString)
            streamWriter.Close()
            Dim response As WebResponse = webRequest.GetResponse()
            Dim responseStream As Stream = response.GetResponseStream()
            Dim responseReader As New StreamReader(responseStream)
            Dim responseString As String = responseReader.ReadToEnd()
            xml = responseString
            Dim xml1 As New XmlDocument
            xml1.LoadXml(responseString)
            Dim node As XmlNodeList = xml1.GetElementsByTagName("original")
            URL = node(0).InnerText
            node = xml1.GetElementsByTagName("delete_page")
            del = node(0).InnerText
        End Sub
        ''' <summary>
        ''' The Upload from Byte Array sub.
        ''' </summary>
        ''' <param name="Data">The Binary Data for the image.</param>
        ''' <param name="APIKey">Your anonymous API Key to ImgUr.</param>
        Sub UploadByte(ByVal Data As Byte(), ByVal APIKey As String)
            Dim uploadRequestString As [String] = URLEncode("image") + "=" + URLEncode(System.Convert.ToBase64String(Data)) + "&" + URLEncode("key") + "=" + URLEncode(APIKey) + "&" + URLEncode("type") + "=" + URLEncode("base64")
            Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://api.imgur.com/2/upload"), HttpWebRequest)
            webRequest.Method = "POST"
            webRequest.ContentType = "application/x-www-form-urlencoded"
            webRequest.ServicePoint.Expect100Continue = False
            Dim streamWriter As New StreamWriter(webRequest.GetRequestStream())
            streamWriter.Write(uploadRequestString)
            streamWriter.Close()
            Dim response As WebResponse = webRequest.GetResponse()
            Dim responseStream As Stream = response.GetResponseStream()
            Dim responseReader As New StreamReader(responseStream)
            Dim responseString As String = responseReader.ReadToEnd()
            xml = responseString
            Dim xml1 As New XmlDocument
            xml1.LoadXml(responseString)
            Dim node As XmlNodeList = xml1.GetElementsByTagName("original")
            url = node(0).InnerText
            node = xml1.GetElementsByTagName("delete_page")
            del = node(0).InnerText
        End Sub
        ''' <summary>
        ''' I found out not everybody has the built-in URL encoder, so i built my own!
        ''' </summary>
        ''' <param name="Text">The text to be encoded.</param>
        ''' <returns>The encoded text.</returns>
        Public Function URLEncode(ByVal Text As String) As String
            Dim i As Integer
            Dim acode As Integer
            URLEncode = Text
            For i = Len(URLEncode) To 1 Step -1
                acode = Asc(Mid$(URLEncode, i, 1))
                Select Case acode
                    Case 48 To 57, 65 To 90, 97 To 122
                    Case 32
                        URLEncode = URLEncode.Substring(0, i - 1) & "+" & Mid(URLEncode, i + 1)
                    Case Else
                        URLEncode = URLEncode.Substring(0, i - 1) & "%" & Hex$(acode) & Mid$(URLEncode, i + 1)
                End Select
            Next
            If InStr(URLEncode, "%D%A") Then
                URLEncode = Replace(URLEncode, "%D%A", "%0D%0A")
            End If
        End Function
    End Class
End Namespace